package com.jacksay.vikingfl.synchro
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.ControlBar;
	import mx.containers.TitleWindow;
	import mx.controls.Button;
	import mx.controls.DataGrid;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.Application;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;

	public class VOModalSynchro extends TitleWindow
	{
		
		[Bindable]
		[ArrayElementType("com.jacksay.vikingfl.synchro.VOSynchonizePipeRepport")]
		public var repports:ArrayCollection;
		
		public var callBackFunction:Function;
		
		public var dg:DataGrid;
		public var bar:ControlBar = new ControlBar();
		public var boutSynchro:Button = new Button();
		
		
		
		
		public function VOModalSynchro()
		{
			super();
			showCloseButton = true;
			title = "Synchronisation des données";
			addEventListener("validChange", validate, false, 0, true);
			addEventListener(CloseEvent.CLOSE, fermeture, false, 0, true);
			
			// datagrid
			dg = new DataGrid();
			dg.percentHeight = 100;
			dg.percentWidth = 100;
			var cols:Array = [];
			
			var colLabel:DataGridColumn = new DataGridColumn("label");
			colLabel.headerText = "Donnée";
			
			var colInitial:DataGridColumn = new DataGridColumn("initial");
			colInitial.headerText = "Ancienne valeur";
			
			var colIncoming:DataGridColumn = new DataGridColumn("incoming");
			colIncoming.headerText = "Valeur entrante";
			
			var colUser:DataGridColumn = new DataGridColumn("user");
			colUser.headerText = "Votre valeur";
			
			var colAction:DataGridColumn = new DataGridColumn("choix");
			colAction.headerText = "Action";
			colAction.itemRenderer = new VOResolveCombo();
			colAction.rendererIsEditor = true;
			
			dg.columns = [colLabel, colInitial, colIncoming, colUser, colAction];
			
			addChild(dg);
			
			boutSynchro.label = "Synchroniser";
			boutSynchro.addEventListener(MouseEvent.CLICK, validate);
			
			bar.percentWidth = 100;
			bar.height = 30;
			bar.setStyle("horizontalAlign","center");
			bar.addChild(boutSynchro);
			
			addChild(bar);
			
			
		}
		
		override protected function createChildren():void {
			super.createChildren();
			
		}
		
		protected function validate( e:Event ) :void {
			var repport:VOSynchonizePipeRepport;
			for each( repport in repports ){
				repport.pipe.resolveSynchro( repport.choix );
			}
			callBackFunction();
			fermeture(null);
		}
		
		public static function show( repports:Array, callBackFunction:Function ) :void {
			var screenRepport:VOModalSynchro = new VOModalSynchro();
			screenRepport.repports = new ArrayCollection(repports)
			screenRepport.dg.dataProvider = screenRepport.repports;
			screenRepport.callBackFunction = callBackFunction;
			screenRepport.width = 650;
			screenRepport.height = 400;
			
			PopUpManager.addPopUp(screenRepport, Application.application as DisplayObject, true);
			PopUpManager.centerPopUp(screenRepport);
		}
		
		protected function fermeture( e:CloseEvent ):void {
			PopUpManager.removePopUp(this);
		}
		
	}
}